using System;
using System.Web;

namespace ASPNETVN.PORTAL.Components.Helpers
{
    public enum ControlTypes
    { 
        
    }
    public class HttpUrlBuilder
    {
        /// <summary>
        /// Generate url for loading module page control.
        /// </summary>
        public static string BuildModulePageUrl(Guid moduleID, int itemID, Enum page)
        {
            return buildUrl("~/Default.aspx", moduleID, itemID, "page=" + page);
        }

        public static string BuildModulePageUrl(Guid moduleID, string itemID, Enum page)
        {
            return buildUrl("~/Default.aspx", moduleID, itemID, "page=" + page);
        }

        public static string BuildModulePageUrl(Guid moduleID, int itemID, Enum page, string format, params object[] args)
        {
            return buildUrl("~/Default.aspx", moduleID, itemID, string.Format(format, args) + "&page=" + page);
        }
        
        //-----------------------------------------------------------------------------------------
        public static string BuildShareUrl(Guid moduleID, int itemID, string controlName)
        {
            string s = buildUrl("~/Default.aspx", moduleID, itemID, "", "");
            s += "&sid=" + controlName;
            return s;
        }

        public static string BuildShareUrl(Guid moduleID, int itemID, string controlName, string format, params object[] args)
        {
            string s = buildUrl("~/Default.aspx", moduleID, itemID, format, args);
            s += "&sid=" + controlName;
            return s;
        }

        public static string BuildAdministrationModuleUrl(Guid moduleID, string controlName)
        {
            string s = buildUrl("~/Default.aspx", moduleID, 0, "", "");
            s += "&amcid=" + controlName;
            return s;
        }

        public static string BuildAdministrationModuleUrl(Guid moduleID, string controlName, string format, params object[] args)
        {
            string s = buildUrl("~/Default.aspx", moduleID, 0, format, args);
            s += "&amcid=" + controlName;
            return s;
        }

        public static string BuildUrl(Guid moduleID, int itemID, string controlName)
        {
            string s = buildUrl("~/Default.aspx", moduleID, itemID, "", "");
            s += "&ncid=" + controlName;
            return s;
        }


        public static string BuildUrl(Guid moduleID, int itemID, string controlName, string format, params object[] args)
        {
            string s = buildUrl("~/Default.aspx", moduleID, itemID, format, args);
            s += "&ncid=" + controlName;
            return s;
        }

        public static string BuildAddEditUrl(Guid moduleID, int itemID, string controlName)
        {
            string s = buildUrl("~/Default.aspx", moduleID, itemID, "", "");
            s += "&aecid=" + controlName;
            return s;
        }

        public static string BuildAddEditUrl(Guid moduleID, int itemID, string controlName, string format, params object[] args)
        {
            string s = buildUrl("~/Default.aspx", moduleID, itemID, format, args);
            s += "&aecid=" + controlName;
            return s;
        }
        
        private static string buildUrl(string page, Guid moduleID, int itemID, string format, params object[] args)
        {
            string url = page;
            HttpContext context = HttpContext.Current;

            // TabID.
            Guid tabID = Global.DefaultTabID;
            if (context.Request.QueryString["tabid"] != null) tabID = new Guid(context.Request.QueryString["tabid"]);

            url += string.Format("?tabid={0}", tabID);

            // ModuleID.
            if (moduleID != null) url += string.Format("&mid={0}", moduleID);

            // ItemID.
            if (itemID > 0) url += string.Format("&itemid={0}", itemID);

            if (!string.IsNullOrEmpty(format)) url += string.Format("&" + format, args);

            // ReturnUrl.
            string returnUrl = string.Empty;
            if (context.Request.QueryString["ReturnUrl"] != null) 
                returnUrl = context.Request.QueryString["ReturnUrl"];
            
            if (!string.IsNullOrEmpty(returnUrl))
                url += string.Format("&returnurl={0}", context.Server.UrlEncode(Helpers.EncryptHelper.Encrypt(context.Request.RawUrl)));


            //string returnUrl = string.Empty;
            //if (context.Request.QueryString["ReturnUrl"] != null) returnUrl = context.Request.QueryString["ReturnUrl"];
            //if (!string.IsNullOrEmpty(returnUrl))
            //{
            //    url += string.Format("&returnurl={0}", context.Server.UrlEncode(context.Request.RawUrl));
            //}
            return url;
        }

        private static string buildUrl(string page, Guid moduleID, string itemID, string format, params object[] args)
        {
            string url = page;
            HttpContext context = HttpContext.Current;

            // TabID.
            Guid tabID = Global.DefaultTabID;
            if (context.Request.QueryString["tabid"] != null) tabID = new Guid(context.Request.QueryString["tabid"]);

            url += string.Format("?tabid={0}", tabID);

            // ModuleID.
            if (moduleID != null) url += string.Format("&mid={0}", moduleID);

            // ItemID.
            if (itemID !="") url += string.Format("&itemid={0}", itemID);

            if (!string.IsNullOrEmpty(format)) url += string.Format("&" + format, args);

            // ReturnUrl.
            string returnUrl = string.Empty;
            if (context.Request.QueryString["ReturnUrl"] != null)
                returnUrl = context.Request.QueryString["ReturnUrl"];

            if (!string.IsNullOrEmpty(returnUrl))
                url += string.Format("&returnurl={0}", context.Server.UrlEncode(Helpers.EncryptHelper.Encrypt(context.Request.RawUrl)));


            //string returnUrl = string.Empty;
            //if (context.Request.QueryString["ReturnUrl"] != null) returnUrl = context.Request.QueryString["ReturnUrl"];
            //if (!string.IsNullOrEmpty(returnUrl))
            //{
            //    url += string.Format("&returnurl={0}", context.Server.UrlEncode(context.Request.RawUrl));
            //}
            return url;
        }
    }
}
